import {Component, OnInit, ViewChild, ViewChildren} from "@angular/core";
import { UploadFile } from 'ng-zorro-antd';
import {HttpClient} from "@angular/common/http";
import {AppUrl} from "../../../../app.url";
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {UniversityComponent} from "../campusApply/university.component";


@Component({
    templateUrl:'accInfo.component.html',
    selector:'accInfo-list'
})

export class AccInfoComponent implements OnInit{

    studentid:number;
    username:string;
    phone:number;
    switchValue:boolean;
    isVisible1:boolean;
    routename:string;
    univer: any[];
    province:any=[];
    placeList:any[];
    cityList:any[];
    campusList:any[];
    department:string;
    schoolid:number;
    areaid:number;
    campusid:number;
    positionid:number;
    noticeuni:string;
    noticeCampus:string;
    school:string;
    schoolList:any[];
    _value:string;
    campusName:string;
    area:any[];
    areaName:string;
    addOrReturn:boolean;
    camarea:boolean;
    prOpt:any=[];
    checkOptions:any[];
    position:Array<{value:string,option:string}>;
    fileList = [{
        uid: -1,
        name: 'xxx.png',
        status: 'done',
        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
    }];
    previewImage = '';
    previewVisible = false;
    radioValue='';//性别
    entrance:any;//入学年份
    majorsub:string;//大学专业
    qq:number;//qq号码

    cOpt:string;
    newarea:string;
    addAreaPro:string;
    addAreaCity:string;
    newcamname:string;
    newcamtype:number;
    newcamlevel:number;
    newcamscale:number;
    newcamintroduce:string;
    newfileList = [{
        uid: -1,
        name: 'xxx.png',
        status: 'done',
        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
    }];
    campustypeList:any=[];
    campuslevelList:any=[];

    uploadUrl:string;

    constructor(public http:HttpClient){
        this.username='Long1';
        this.phone=13809872345;
        this.switchValue=false;
        this.isVisible1=false;

        this.checkOptions=[
            {label: '文艺汇演', value: '1', checked: false},
            {label: '体育竞技', value: '2', checked: false},
            {label: '社会实践', value: '3', checked: false},
            {label: '设计开发', value: '4', checked: false},
            {label: '讲座论坛', value: '5', checked: false},
            {label: '迎新招新', value: '6', checked: false},
            {label: '演讲辩论', value: '7', checked: false},
            {label: '创业营销', value: '8', checked: false},
            {label: '传单派送', value: '9', checked: false},
            {label: '高校推广', value: '10', checked: false},
            {label: '影视音乐', value: '11', checked: false}
        ];
        this.addOrReturn=true;
        this.camarea=true;

        this.position=[
            {value:'1',option:'主席团'},
            {value:'2',option:'部长'},
            {value:'3',option:'副部长'},
            {value:'4',option:'干事'}
        ];
        this.uploadUrl=AppUrl.MemberHeadImg.upoadUrl;
        this.http.post(AppUrl.student.urlsel, {userid:localStorage.getItem('userid')}).subscribe(data => {
            let temp:any;
            temp=data;
            console.info(data);
            this.username=temp.data[0].username;
            this.phone=temp.data[0].phone;
            this.radioValue=temp.data[0].sex;
            this.studentid=temp.data[0].id;
            let authforview=temp.data[0].authforview;
            if(authforview=='0'){
                this.switchValue=true;
            }else{
                this.switchValue=false;
            }
        });
    }

    handlePreview = (file: UploadFile) => {
        this.previewImage = file.url || file.thumbUrl;
        this.previewVisible = true;
    }

    updateStat(item){
        console.log(item);
        item.checked=!item.checked;
    }

    ngOnInit(){}

    showModal(item){
        this.isVisible1=true;
        this.routename=item;
        if(item=='university'){
            /*
            *一次请求后,收到省和市的信息
            * data.data数组的内容是{id:number,province:string,city:string}
             */
            if(this.province.length==0){
                this.http.post(AppUrl.place.cityUrl,{}).subscribe(data=>{
                    console.log(data);
                    let temp1:any;
                    temp1=data;
                    let temp=temp1.data;
                    this.placeList=temp1.data;
                    let i=1;
                    this.province.push({
                        id:temp[0].id,
                        province:temp[0].province
                    });
                    for(let place of temp){
                        if(this.province[i-1].id==place.id){
                            continue;
                        }else{
                            this.province.push({
                                id:place.id,
                                province:place.province
                            });
                            i++;
                        }
                    }
                });
            }
            this.noticeuni='若没有你的学校，请联系管理员添加 QQ: 41176123 微信: xinyizanzhu';
            this.noticeCampus=null;
        }else if(item=='area'){
            if(this.school==null){
                this.routename=null;
                alert('请先选择学校!');
                this.isVisible1=false;
                return;
            }else{
                // this.area=['本部','呈贡'];
                this.http.post(AppUrl.place.areaUrl,{universityid:this.schoolid}).subscribe(data=>{
                    console.log(data);
                    let temp:any=data;
                    this.area=temp.data;
                });
                this.noticeCampus='若列表中没有你的校区，请手动输入';
                this.noticeuni=null;
            }

        }else{
            if(this.areaName==null){
                this.routename=null;
                alert('请先选择校区!');
                this.isVisible1=false;
                return;
            }else{
                this.http.post(AppUrl.campus.urlSearch,{areaid:this.areaid}).subscribe(data=>{
                    console.log(data);
                    let temp1:any=data;
                    let temp:any[]=temp1.data;
                    this.campusList=temp;
                });
                this.noticeuni=null;
                this.noticeCampus=null;
            }
        }
    }
    handleCancel(ev){
        this.isVisible1=false;
    }

    handleOk(ev){
        this.isVisible1=false;
    }

    _console(ev){
        this.schoolList=[
            'parking university','yunnan university','sichuan university',
            'suh university','sdf university'
        ];
        console.log('------value='+this._value);
    }

    chooseSch(data){
        this.isVisible1=false;
        this.school=data.universityname;
        this.schoolid=data.id;
    }

    chooseCam(data){
        this.isVisible1=false;
        this.campusName=data.campusname;
        this.campusid=data.id;
    }

    chooseArea(data){
        this.isVisible1=false;
        this.areaName=data.areaname;
        this.areaid=data.id;
    }

    onSearch(ev){

    }

    addCampus(){
        this.addOrReturn=false;
        if(this.campustypeList.length==0){
            this.http.post(AppUrl.campus.typeAndlevel,{}).subscribe(data=>{
                console.log(data);
                let temp:any=data;
                let temp1:any=[];
                temp1=temp.data;
                this.campustypeList=temp1[1];
                this.campuslevelList=temp1[0];
            })
        }
    }

    returnCamList(){
        this.addOrReturn=true;
    }

    campusArea(){
        this.camarea=!this.camarea;
    }

    chooseProvince(item,flag){
        console.log('item='+item);
        this.cityList=this.placeList.filter(e=>e.id==item.id);
        if(flag==0){
            this.http.post(AppUrl.univeristy.universityUrl,{proid:this.prOpt.id}).subscribe(data=>{
                console.log(data);
                let temp:any=data;
                this.schoolList=temp.data;
            })
        }
    }

    submit(){
        let authforview:string;
        if(this.switchValue){
            authforview='0';
        }else{
            authforview='1';
        }
        this.http.post(AppUrl.student.urlup, {
            id:this.studentid,userid:localStorage.getItem('userid'),username:this.username,phone:this.phone,
            sex:this.radioValue,entrance:this.entrance, majorsub:this.majorsub,qq:this.qq,
            universityid:this.schoolid,areaid:this.areaid,campusid:this.campusid,
            department:this.department,positionid:this.positionid,special:'1',authforview:authforview
        }).subscribe(data => {
            console.log('------'+data);
        });
    }


    confirmAddArea(){
        //增加校区
        this.http.post(AppUrl.place.areaAddUrl,{areaname:this.newarea,universityid:this.schoolid}).subscribe(data=>{
            let temp:any=data;
            this.area=temp.data;
        })
    }

    confirmAddCampus(){
        //增加新社团,社团信息包括:社团名称，学校id,校区id，社团类型，社团简介，社团性质(级别)，社团规模
        this.http.post(AppUrl.campus.addNewCampus,{campusname:this.newcamname,
            universityid:this.schoolid,areaid:this.areaid,campustype:this.newcamtype,
            introduce:this.newcamintroduce,level:this.newcamlevel,scale:this.newcamscale
        }).subscribe(data=>{
            let temp:any=data;
            this.campusList=temp.data;
        });
    }
}
